package com.hhp.hhptt.utils;
import com.google.gson.Gson;
import com.hhp.hhptt.entity.audioEntity;
import com.hhp.hhptt.entity.pictureEntity;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class commonUtils {

    public static void listFiles(File folder, ArrayList<Object>audio, ArrayList<Object>picture) {


        // 获取文件夹中的所有文件和子文件夹
        File[] files = folder.listFiles();

        // 遍历文件和子文件夹
        if (files != null) {
            for (File file : files) {
                // 如果是文件，则打印文件路径
                if (file.isFile()) {
                    //System.out.println("文件: " + file.getAbsolutePath());

                    if (isImage(file.getAbsolutePath())){
                        //图片
                        pictureEntity pictureEntity = new pictureEntity();
                        pictureEntity.setName(file.getAbsoluteFile().toString());
                        Gson gson = new Gson();
                        String json = gson.toJson(pictureEntity);
                        picture.add(json);
                    }
                    if (isAudio(file.getAbsolutePath())){
                        //音频
                        audioEntity audioEntity = new audioEntity();
                        audioEntity.setName(file.getAbsoluteFile().toString());
                        Gson gson = new Gson();
                        String json = gson.toJson(audioEntity);
                        audio.add(json);
                    }
                }
                // 如果是目录，则递归调用 listFiles 方法
                else if (file.isDirectory()) {
                    listFiles(file,audio,picture);
                }
            }
        }
    }
    // 判断是否图片文件
    public static boolean isImage(String filename) {
        String regex = "(?i).+\\.(jpg|jpeg|png|gif|bmp)$"; // 忽略大小写
        return filename.matches(regex);
    }

    // 判断是否音频文件
    public static boolean isAudio(String filename) {
        String regex = "(?i).+\\.(m4a|mp3|wav|flac|aac|ogg)$"; // 忽略大小写
        return filename.matches(regex);
    }


    public static String getFileAbsolutePath(String fileName) throws URISyntaxException, MalformedURLException {
        File dataDir=null;
        String absolutePath="";
        URL jarUrl = commonUtils.class.getProtectionDomain().getCodeSource().getLocation();
        if (jarUrl.getProtocol().equals("jar")) {
            String regex = "^jar:(file:/.+?)!.*$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(jarUrl.toString());

            if (matcher.find()) {
                // 提取匹配到的路径
                String cleanedPath = matcher.group(1);
                URL fileUrl = new URL(cleanedPath);
                File jarFile2 = new File(fileUrl.toURI());
                String jarDir = jarFile2.getParentFile().getPath();
                // 构造与JAR文件同级的 data 文件夹路径
                dataDir = new File(jarDir + File.separator + fileName);
                absolutePath = dataDir.getAbsolutePath();
                absolutePath = absolutePath.replace("\\", "\\\\");
            }
        } else {
            // 获取当前类的ProtectionDomain，并从中获取JAR文件路径
            File jarFile = new File(jarUrl.toURI());
            // 获取JAR文件所在目录
            String jarDir = jarFile.getParentFile().getParentFile().getParentFile().getPath();
            // 构造与JAR文件同级的 data 文件夹路径
            dataDir = new File(jarDir + File.separator + fileName);
            absolutePath = dataDir.getAbsolutePath();
            absolutePath = absolutePath.replace("\\", "\\\\");
        }
        return absolutePath;
    }


    public static String getFileAbsolutePathDeep(String firstLevelCal,String secondLevelCal,String threeLevelCal,String FourLevelCal) throws URISyntaxException, MalformedURLException {
        File dataDir=null;
        String absolutePath="";
        URL jarUrl = commonUtils.class.getProtectionDomain().getCodeSource().getLocation();
        if (jarUrl.getProtocol().equals("jar")) {
            String regex = "^jar:(file:/.+?)!.*$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(jarUrl.toString());

            if (matcher.find()) {
                // 提取匹配到的路径
                String cleanedPath = matcher.group(1);
                URL fileUrl = new URL(cleanedPath);
                File jarFile2 = new File(fileUrl.toURI());
                String jarDir = jarFile2.getParentFile().getPath();
                // 构造与JAR文件同级的 data 文件夹路径
//                dataDir = new File(jarDir + File.separator + fileName);
                //dataDir = new File(jarDir + File.separator + firstLevelCal);
                if (!FourLevelCal.equals("") && FourLevelCal!=null){
                    dataDir = new File(jarDir + File.separator + firstLevelCal+
                            File.separator+secondLevelCal+File.separator+threeLevelCal+File.separator+FourLevelCal);
                } else if (!threeLevelCal.equals("") && threeLevelCal!=null) {
                    dataDir = new File(jarDir + File.separator + firstLevelCal+
                            File.separator+secondLevelCal+File.separator+threeLevelCal);
                }else if (!secondLevelCal.equals("") && secondLevelCal!=null) {
                    dataDir = new File(jarDir + File.separator + firstLevelCal+
                            File.separator+secondLevelCal);
                }else if (!firstLevelCal.equals("") && firstLevelCal!=null) {
                    dataDir = new File(jarDir + File.separator + firstLevelCal);
                }

                absolutePath = dataDir.getAbsolutePath();
                absolutePath = absolutePath.replace("\\", "\\\\");
            }
        } else {
            // 获取当前类的ProtectionDomain，并从中获取JAR文件路径
            File jarFile = new File(jarUrl.toURI());
            // 获取JAR文件所在目录
            //String jarDir = jarFile.getParentFile().getParentFile().getParentFile().getPath();
            String jarDir = jarFile.getParentFile().getParentFile().getParentFile().getPath();
//            System.out.println("jarDir:"+jarDir);

            // 构造与JAR文件同级的 data 文件夹路径
            if (!FourLevelCal.equals("") && FourLevelCal!=null){
                dataDir = new File(jarDir + File.separator + firstLevelCal+
                        File.separator+secondLevelCal+File.separator+threeLevelCal+File.separator+FourLevelCal);
            } else if (!threeLevelCal.equals("") && threeLevelCal!=null) {
                dataDir = new File(jarDir + File.separator + firstLevelCal+
                        File.separator+secondLevelCal+File.separator+threeLevelCal);
            }else if (!secondLevelCal.equals("") && secondLevelCal!=null) {
                dataDir = new File(jarDir + File.separator + firstLevelCal+
                        File.separator+secondLevelCal);
            }else if (!firstLevelCal.equals("") && firstLevelCal!=null) {
                dataDir = new File(jarDir + File.separator + firstLevelCal);
            }

//            System.out.println("dataDir:"+dataDir);
            absolutePath = dataDir.getAbsolutePath();
            absolutePath = absolutePath.replace("\\", "\\\\");
        }
        return absolutePath;
    }
}
